
/* nixonFTP
 * Copyright (C) 2010 NIXON Development Corporation.
 * All rights reserved.
 * http://members.shaw.ca/nixon.com
*/

package nixonftp.ui;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.text.DateFormat;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import nixonftp.NXFormat;
import nixonftp.NXObjectIndex;
import static nixonftp.NXObjectIndex.*;

public class NXDetailsDialog extends JDialog {
	private String msgDirectory = "/";
	NXObjectIndex moiObject;
	ContentLabel mlbTitle;
	ContentLabel mlbKind;
	JLabel mlbType;
	Label mlbMimeLabel;
	ContentLabel mlbMime;
	ContentLabel mlbWhere;
	ContentLabel mlbLinkLocation;
	Label mlbLinkLabel;
	ContentLabel mlbSize;
	JLabel mlbSizeBytes;
	ContentLabel mlbModified;
	JLabel mlbModifiedRelative;

	Label mlbOwnerLabel;
	ContentLabel mlbOwner;
	Label mlbGroupLabel;
	ContentLabel mlbGroup;

	JCheckBox mcbOwnerRead;
	JCheckBox mcbOwnerWrite;
	JCheckBox mcbOwnerExecute;

	JCheckBox mcbGroupRead;
	JCheckBox mcbGroupWrite;
	JCheckBox mcbGroupExecute;

	JCheckBox mcbOtherRead;
	JCheckBox mcbOtherWrite;
	JCheckBox mcbOtherExecute;

	public void setDirectory(String s) {
		msgDirectory = s;
	}

	public NXDetailsDialog(JFrame frParent) {
		super(frParent);
		setTitle("Details");

		//Container c = getContentPane();
		//setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
		JPanel panel = new JPanel();
		add(panel);

		GridBagLayout gl = new GridBagLayout();
		panel.setLayout(gl);
		GridBagConstraints gcLabels = new GridBagConstraints();
		GridBagConstraints gcContent = new GridBagConstraints();
		
		mlbTitle = new ContentLabel();
		mlbTitle.setFont(mlbTitle.getFont().deriveFont(Font.BOLD));
		mlbKind = new ContentLabel();
		mlbType = new JLabel();
		mlbType.setForeground(Color.GRAY);
		mlbMime = new ContentLabel();
		mlbWhere = new ContentLabel();
		mlbLinkLocation = new ContentLabel();
		mlbSize = new ContentLabel();
		mlbSizeBytes = new JLabel();
		mlbSizeBytes.setForeground(Color.GRAY);
		mlbModified = new ContentLabel();
		mlbModifiedRelative = new JLabel();
		mlbModifiedRelative.setForeground(Color.GRAY);
		mlbOwner = new ContentLabel();
		mlbGroup = new ContentLabel();
		JPanel kindPanel = new JPanel();
		JPanel sizePanel = new JPanel();
		JPanel datePanel = new JPanel();

		//c.fill = GridBagConstraints.HORIZONTAL;
		gcLabels.weightx = 0.2;
		gcLabels.gridx = 0;
		gcLabels.gridy = 0;
		gcLabels.insets = new Insets(0, 12, 4, 12);
		gcLabels.anchor = GridBagConstraints.LINE_END;
		panel.add(new Label("Name: "), gcLabels);

		gcContent.weightx = 0.8;
		gcContent.gridx = 1;
		gcContent.gridy = 0;
		gcContent.anchor = GridBagConstraints.LINE_START;
		gcContent.insets = new Insets(0, 0, 4, 12);
		panel.add(mlbTitle, gcContent);

		gcLabels.gridy = 1;
		panel.add(new Label("Kind: "), gcLabels);

		gcContent.weightx = 0.8;
		gcContent.gridx = 1;
		gcContent.gridy = 1;
		kindPanel.setLayout(new BoxLayout(kindPanel, BoxLayout.PAGE_AXIS));
		kindPanel.add(mlbKind);
		kindPanel.add(mlbType);
		panel.add(kindPanel, gcContent);

		gcLabels.gridy = 2;
		mlbMimeLabel = new Label("MIME type: ");
		panel.add(mlbMimeLabel, gcLabels);
		gcContent.gridy = 2;
		panel.add(mlbMime, gcContent);

		gcLabels.gridy = 3;
		panel.add(new Label("Where: "), gcLabels);
		gcContent.gridy = 3;
		panel.add(mlbWhere, gcContent);

		gcLabels.gridy = 4;
		mlbLinkLabel = new Label("Link to: ");
		panel.add(mlbLinkLabel, gcLabels);

		gcContent.gridy = 4;
		panel.add(mlbLinkLocation, gcContent);

		gcLabels.gridy = 5;
		panel.add(new Label("Size: "), gcLabels);

		gcContent.gridy = 5;
		sizePanel.setLayout(new BoxLayout(sizePanel, BoxLayout.PAGE_AXIS));
		sizePanel.add(mlbSize);
		sizePanel.add(mlbSizeBytes);
		panel.add(sizePanel, gcContent);

		gcLabels.gridy = 6;
		panel.add(new Label("Modified: "), gcLabels);

		gcContent.gridy = 6;
		datePanel.setLayout(new BoxLayout(datePanel, BoxLayout.PAGE_AXIS));
		datePanel.add(mlbModified);
		datePanel.add(mlbModifiedRelative);
		panel.add(datePanel, gcContent);

		gcLabels.gridy = 7;
		mlbOwnerLabel = new Label("Owner: ");
		panel.add(mlbOwnerLabel, gcLabels);

		gcContent.gridy = 7;
		panel.add(mlbOwner, gcContent);

		gcLabels.gridy = 8;
		mlbGroupLabel = new Label("Group: ");
		panel.add(mlbGroupLabel, gcLabels);

		gcContent.gridy = 8;
		panel.add(mlbGroup, gcContent);

		gcContent.gridy = 9;
		gcContent.gridx = 0;
		gcContent.gridwidth = 2;
		gcContent.weightx = 1;
		gcContent.weighty = 1;
		gcContent.anchor = GridBagConstraints.CENTER;
		JPanel permissions = new JPanel();
		panel.add(permissions, gcContent);

		permissions.setLayout(new GridLayout(4, 4, 4, 4));

		permissions.add(new JLabel(""));
		permissions.add(new CenterLabel("Read"));
		permissions.add(new CenterLabel("Write"));
		permissions.add(new CenterLabel("Execute"));

		JLabel lbOwner = (JLabel) permissions.add(new JLabel("Owner"));
		lbOwner.setHorizontalAlignment(SwingConstants.RIGHT);
		permissions.add(mcbOwnerRead = new CheckBox());
		permissions.add(mcbOwnerWrite = new CheckBox());
		permissions.add(mcbOwnerExecute = new CheckBox());

		JLabel lbGroup = (JLabel) permissions.add(new JLabel("Group"));
		lbGroup.setHorizontalAlignment(SwingConstants.RIGHT);
		permissions.add(mcbGroupRead = new CheckBox());
		permissions.add(mcbGroupWrite = new CheckBox());
		permissions.add(mcbGroupExecute = new CheckBox());

		JLabel lbOther = (JLabel) permissions.add(new JLabel("Other"));
		lbOther.setHorizontalAlignment(SwingConstants.RIGHT);
		permissions.add(mcbOtherRead = new CheckBox());
		permissions.add(mcbOtherWrite = new CheckBox());
		permissions.add(mcbOtherExecute = new CheckBox());
	}

	private class Label extends JLabel {
		Label(String l) {
			super(l);
		}
	}

	private class CenterLabel extends JLabel {
		CenterLabel(String l) {
			super(l);
			setHorizontalAlignment(SwingConstants.CENTER);
		}
	}
	
	private class CheckBox extends JCheckBox {
		CheckBox() {
			super();
			setHorizontalAlignment(SwingConstants.CENTER);
		}
	}

	private class ContentLabel extends JLabel {
		ContentLabel() {
			super();
			Font font = new Font("Dialog", Font.PLAIN, 16);
			setFont(font);
		}
	}

	public void setObject(NXObjectIndex o) {
		moiObject = o;
		if (o == null) return;
		setTitle("Details for " + o.name);
		mlbTitle.setText(o.name);
		boolean blFile = o.type == NXObjectIndex.TYPE_FILE;

		if (blFile) {
			String sgExt = NXFormat.getExtension(o.name);
			if (sgExt.isEmpty()) {
				mlbKind.setText("File");
			} else {
				mlbKind.setText(sgExt.toUpperCase() + " File");
			}
			String[] type = NXFormat.getTypeFromExtension(sgExt);
			mlbType.setText(type[0]);
			if (type[1].isEmpty()) {
				mlbMimeLabel.setVisible(false);
				mlbMime.setVisible(false);
			} else {
				mlbMime.setText(type[1]);
				mlbMimeLabel.setVisible(true);
				mlbMime.setVisible(true);
			}
			mlbType.setVisible(true);
		} else {
			mlbKind.setText(o.getKind());
			mlbType.setVisible(false);
			mlbMimeLabel.setVisible(false);
			mlbMime.setVisible(false);
		}

		mlbOwner.setText(o.owner);
		mlbGroup.setText(o.group);
		boolean blOwner = !o.owner.isEmpty();
		mlbOwner.setVisible(blOwner);
		mlbOwnerLabel.setVisible(blOwner);
		boolean blGroup = !o.group.isEmpty();
		mlbGroup.setVisible(blGroup);
		mlbGroupLabel.setVisible(blGroup);
		
		boolean blLink = o.type == NXObjectIndex.TYPE_LINK;
		mlbLinkLocation.setVisible(blLink);
		mlbLinkLabel.setVisible(blLink);
		if (blLink) { //show link location
			mlbLinkLocation.setText(o.realLoc);
		}
		setDirectoryText();
		mlbSize.setText(NXFormat.fileSize(o.size, true));
		mlbSizeBytes.setText(o.size + " bytes");
		mlbModified.setText(DateFormat.getDateTimeInstance().format(o.date));
		mlbModifiedRelative.setText(NXFormat.parseDate(o.date));

		mcbOwnerRead.setSelected(o.canDo(OWNER, READ));
		mcbOwnerWrite.setSelected(o.canDo(OWNER, WRITE));
		mcbOwnerExecute.setSelected(o.canDo(OWNER, EXECUTE));

		mcbGroupRead.setSelected(o.canDo(GROUP, READ));
		mcbGroupWrite.setSelected(o.canDo(GROUP, WRITE));
		mcbGroupExecute.setSelected(o.canDo(GROUP, EXECUTE));

		mcbOtherRead.setSelected(o.canDo(OTHER, READ));
		mcbOtherWrite.setSelected(o.canDo(OTHER, WRITE));
		mcbOtherExecute.setSelected(o.canDo(OTHER, EXECUTE));
		this.pack();
	}

	private void setDirectoryText() {
		if (msgDirectory == null || msgDirectory.isEmpty()) {
			mlbWhere.setText("/");
		} else {
			mlbWhere.setText(msgDirectory);
		}
	}
}
